// pages/register/register.js
let httptool = require("../../utils/zry.httptool.js");
var md5 = require('../../utils/md5.js');
var base64 = require('../../utils/base64.js');  
let app = getApp(); 
Page({
  data:{
      customer:{},
      radioCheckVal:'男',
      step:1,
      TelIsUsed:false,
      codeBtnText:"获取验证码",
      stepName:"下一步",
      Code:'',/*短信验证码*/
      confirmPwd:'',/*确认密码*/
      provinces:[],
      province:'请选择',
      citys:[],
      city:'请选择',
      selectProvince:true,
      selectCity:true,
      selectArea:false,
      left:false,
      right:false,
      msgprop:'',//消息提示 
  },
  telChanage:function(e){
    this.data.customer.Tel = e.detail.value;
    this.setData({ customer: this.data.customer});
  },
  valCode:function(e){
      let code=e.detail.value;
      this.setData({Code:code});
  },
  pwdCheck:function(e){
    this.data.customer.Pwd=e.detail.value;
    this.setData({ customer: this.data.customer});
  },
  confirmpwdCheck:function(e){
      let confirmpwd=e.detail.value;
      this.setData({confirmPwd:confirmpwd});
  },
  clearTel:function(e){//清除输入的手机号
    this.data.customer.Tel = '';
    this.setData({customer: this.data.customer});
  },
  clearPwd:function(e){//清除输入的密码
    this.data.customer.Pwd = '';
    this.setData({customer: this.data.customer});
  },
  clearCpwd:function(e){//清除输入的密码
    let confirmpwd='';
    this.setData({confirmPwd:confirmpwd});
  },
  nextClick:function(){
     let self=this;
     if(this.data.step==1){  
       this.step1(self);
     }else if(this.data.step==2){
       this.step2(self);
     }else if(this.data.step==3){
       this.step3(self);
     }
  },
  step1: function (self){
    if (self.data.customer.Tel == "") {
      app.showNotify(that, "请输入手机号！");
      return false;
    }
    if (!app.validate(self.data.customer.Tel, 'phone')) {
      app.showNotify(that, "手机号不正确");
      return false;
    }
    httptool.ajaxBasePost("api/Customer/CheckMobileIsExist", { Tel: self.data.customer.Tel }, 
    function (res) {
      if (res.suc) {
        self.getCode();
        self.setData({ step:2, TelIsUsed: !res.data });
        if (!self.data.TelIsUsed) {
          self.setData({ stepName: "下一步" });
        } else {
          self.setData({ stepName: "绑定" });
        }
      } else {
         app.showNotify(that, res.msg);
      }
    });
  },
  step2:function(self){
    if (self.data.Code == "") {
      app.showNotify(that, "验证码不能为空！");
      return false;
    }
    var param = {};
    param.Tel = self.data.customer.Tel;
    param.Code = self.data.Code;
    httptool.ajaxBasePost("api/Common/CheckValidateCode", param, function (res) {
      if (res.suc) {
        if (self.data.TelIsUsed) {
          var params = {};
          params.Tel = self.data.Tel;
          params.OpenId = self.data.customer.OpenId;
          params.UnionId = self.data.customer.UnionId;
          params.Tel = self.data.customer.Tel;
          params.ClientId = "62ccad6d-9611-41c5-96f6-08ec0ada0f68";
          params.ClientSecret = "5678";
          params.TokenType=2;
          httptool.ajaxBasePost("/api/Customer/UpdateCustomerWxInfo", params, function (res) {
            if(res.suc){
              app.showSuccess({ msg: "绑定成功" });
              httptool.setCache(res.data);
              wx.redirectTo({
                url: '/pages/index/index',
              });
            }else{
              app.showSuccess({ msg: "绑定失败" });   
            }            
          });
        } else {
          self.setData({ step: 3, stepName: "下一步" });
        }
      } else {
        app.showNotify(self, res.msg);
        return false;
      }      
    });
  },
  step3: function (self){
    if (self.data.customer.Pwd == "") {
      app.showNotify(self, "密码不能为空！");
      return false;
    } else if(self.data.customer.Pwd.length <8){
      app.showNotify(self,"密码不得小于8位！" );
      return false;
    }else if(self.data.confirmPwd.length<8){
      app.showNotify(self, "密码不得小于8位！");
      return false;
    }else if (self.data.confirmPwd == "") {
      app.showNotify(self, "请再次确认密码！");
      return false;
    } else if (self.data.customer.Pwd != self.data.confirmPwd) {
      app.showNotify(self, "密码不一致！");
      return false;
    } else if (!app.validate(self.data.customer.Pwd, 'password')) {
      app.showNotify(self, "密码不规范" );
      return false;
    } else {      
      httptool.ajaxBasePost("/api/Province/GetProvince", { CountryID: 'zh' }, function (res) {
        if (res.suc) {
          self.setData({
            step: 4,
            provinces: res.data,
          });
        } 
      });
    }
  },
  addCustomer: function (e) {/*添加注册用户*/
    let self = this;
    let customer = this.data.customer;
    customer.Pwd = base64.encoder(md5.hexMD5(customer.Pwd));
    customer.ClientId = "62ccad6d-9611-41c5-96f6-08ec0ada0f68";
    customer.ClientSecret = "5678";
    customer.TokenType = 2;
    httptool.ajaxBasePost("/api/Customer/AddCustomer", customer, function (res) {
      if (res.suc) {
        let data = res.data;   
        httptool.setCache(data);
        wx.redirectTo({
          url: '/pages/index/index',
        });
      }
    });
  },
  clickProvince:function(){//省市列表显示
    var selectProvince = this.data.selectProvince;
    if(selectProvince == true){
      this.setData({
          selectArea:true,
          selectProvince:false,
          left:true,
          right:false,
          selectCity:true
        })
    }else{
     this.setData({
        selectArea:false,
        selectProvince:true,
        left:false,
        right:false,
        selectCity:false
      })
    }
  } ,
  clickCity:function(e){
    var selectCity = this.data.selectCity;
    if(selectCity == true){
      this.setData({
          selectArea:true,
          selectCity:false,
          right:true,
          left:false,
          selectProvince:true
        })
    }else{
     this.setData({
        selectArea:false,
        selectCity:true,
        right:false,
        left:false,
        selectProvince:false
      })
    }
  },
    //点击切换
  selectleft:function(e){//省市切换
      let self=this;
      let provinceId=e.target.dataset.id;
      console.log(provinceId);
      httptool.ajaxBasePost("/api/City/GetCity",{ProvinceID:provinceId},function(res){
          if(res.suc){
             self.data.customer.ProvinceId = provinceId;
             self.setData({
                citys:res.data,
                city:'请选择',
                province:e.target.dataset.name,
                selectProvince:true,
                selectArea:false,
                left:false,
                customer: self.data.customer
            });
          }
      });
  },
  selectcity:function(e){
      let self=this;
      self.data.customer.CityId = e.target.dataset.id;
      self.setData({
          city:e.currentTarget.dataset.name,
          customer: self.data.customer,
          selectCity:true,
          selectProvince:true,
          right:false
      });
  },
  getCode:function () {
    if (this.data.codeBtnText =="获取验证码")
    {
      let self=this;
      var param = {};
      param.Tel = this.data.customer.Tel;    
      self.setData({ codeBtnText: "正在获取中" });
      httptool.ajaxBasePost("api/Common/GetMobileCode", param, function (d) {
        console.log(d);
        if (d.suc) {
          var times = 60;
          var timer = setInterval(function () {
            times--;
            self.setData({ codeBtnText: times + "秒后重试" });
            if (times <= 0) {
              self.setData({ codeBtnText: "获取验证码" });
              clearInterval(timer);
              times = 60;
            }
          }, 1000);
        } else {
          app.showNotify(that, res.msg);
        }
      });
    }
  },
  radioCheckedChange:function(e){ 
      console.log('radio发生change事件，携带value值为：', e.detail.value);
      this.setData({ 
        radioCheckVal:e.detail.value
      }) 
    },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数  
    let customer = {};
    customer.Sex = app.getStorage('sex');
    customer.CustomerName = app.getStorage('customerName');
    customer.HeadImg = app.getStorage('headImg');
    customer.OpenId = options.openId;
    customer.UnionId = options.unionId;
    this.setData({ customer: customer}); 
  },
  closeList:function(e){//点击页面的任何地方关闭下拉框
    let that=this;
    that.setData({selectProvince:true,selectCity:true});
  },
  userProfile:function(e){//用户协议
    wx.navigateTo({
      url: '/pages/userProfile/userprofile',
    })
  },
  serviceBtn:function(e){//服务政策
    wx.navigateTo({
      url: '/pages/service/serviceprofile',
    })
  },
  privateProfile:function(e){//隐私政策
    wx.navigateTo({
      url: '/pages/private/privateprofile',
    })
  },
  loginBtn:function(e){
    wx.switchTab({
      url: '/pages/index/index',
    });
  }
})